package com.cloudeasy.user.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.api.vo.user.RoleVO;
import com.cloudeasy.user.dto.RoleDTO;
import com.cloudeasy.user.po.Role;
import com.cloudeasy.user.service.RoleService;
import com.cloudeasy.user.util.IPageUtil;
import com.cloudeasy.user.util.RoleUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/role")
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 获取角色信息
     *
     * @param ids    角色Id列表
     * @param names  角色名称列表
     * @param enable 是否可用
     * @return {@link List<RoleVO>}的{@link Result}
     */
    @GetMapping
    public Result<List<RoleVO>> list(@RequestParam(value = "ids", required = false) List<String> ids,
                                     @RequestParam(value = "names", required = false) List<String> names,
                                     @RequestParam(value = "enable", required = false) Boolean enable) {
        QueryWrapper<Role> queryWrapper = RoleUtil.compose(ids, names, enable);
        return ResultUtil.success(RoleUtil.convert(roleService.list(queryWrapper)));
    }

    /**
     * 分页获取角色信息
     *
     * @param ids    角色Id列表
     * @param names  角色名称列表
     * @param enable 是否可用
     * @param page   页数
     * @param size   每页大小
     * @return {@link List<RoleVO>}的{@link Result}
     */
    @GetMapping("/{page}/{size}")
    public Result<PageResult<RoleVO>> list(@RequestParam(value = "ids", required = false) List<String> ids,
                                           @RequestParam(value = "names", required = false) List<String> names,
                                           @RequestParam(value = "enable", required = false) Boolean enable,
                                           @PathVariable("page") Integer page,
                                           @PathVariable("size") Integer size) {
        QueryWrapper<Role> queryWrapper = RoleUtil.compose(ids, names, enable);
        IPage<Role> roleTempIPage = IPageUtil.compose(page, size);
        return ResultUtil.success(RoleUtil.convert(roleService.page(roleTempIPage, queryWrapper)));
    }

    /**
     * 添加角色
     *
     * @param roleDTO {@link RoleDTO}
     * @return {@link Result<Boolean>}
     */
    @PostMapping
    public Result<Boolean> add(@RequestBody @Validated RoleDTO roleDTO) {
        return ResultUtil.success(roleService.save(roleDTO));
    }

    /**
     * 更新角色
     *
     * @param roleId  角色Id
     * @param roleDTO {@link RoleDTO}
     * @return {@link Result<Boolean>}
     */
    @PutMapping("/{roleId}")
    public Result<Boolean> update(@PathVariable("roleId") String roleId, @RequestBody @Validated RoleDTO roleDTO) {
        return ResultUtil.success(roleService.update(roleId, roleDTO));
    }

    /**
     * 删除角色
     *
     * @param roleId 角色Id
     * @return {@link Result<Boolean>}
     */
    @DeleteMapping("/{roleId}")
    public Result<Boolean> delete(@PathVariable("roleId") String roleId) {
        return ResultUtil.success(roleService.removeById(roleId));
    }
}
